package server;

import java.sql.SQLException;

import database.DatabaseOperationsCapsulesTemporary;
import database.DatabaseOperationsIdentifiers;

public  class Updater {

	private DatabaseOperationsIdentifiers dbOpIdentifiers;
	private DatabaseOperationsCapsulesTemporary dbOpCapsulesTemporary;
	private Boolean accessSynchronized;


    public Updater(DatabaseOperationsIdentifiers dbOpIdentifiers){
		this.dbOpIdentifiers = dbOpIdentifiers;
		this.dbOpCapsulesTemporary = null;
		this.accessSynchronized = new Boolean(true);
	}


	public Updater(DatabaseOperationsIdentifiers dbOpIdentifiers, DatabaseOperationsCapsulesTemporary dbOpCapsulesTemporary){
		this.dbOpIdentifiers = dbOpIdentifiers;
		this.dbOpCapsulesTemporary = dbOpCapsulesTemporary;
		this.accessSynchronized = new Boolean(true);
	}


	public void setNumberCapsules(String email,int id,int numberCapsules)
            throws SQLException{
		synchronized (accessSynchronized) {
			dbOpIdentifiers.setNumberCapsulesIdentifier(email,id,numberCapsules);
		}
	}


    public void setOfferedSpace(String email, int id, long offeredSpace)
            throws SQLException{
		synchronized (accessSynchronized) {
			dbOpIdentifiers.setOfferedSpaceIdentifier(email,id,offeredSpace);
		}
	}


	public void setCapsuleTemporaryComplety(String nameCapsule, String email,int id)
            throws SQLException{
		synchronized (accessSynchronized) {
			dbOpCapsulesTemporary.setCapsuleTemporaryComplety(nameCapsule, email, id);
		}
	}


    public void setUsedSpace(String email, int id, long usedSpace)
            throws SQLException{
		synchronized (accessSynchronized) {
			dbOpIdentifiers.setUsedSpaceIdentifier(email,id,usedSpace);
		}
	}


  	public void close()
            throws SQLException{
		synchronized (accessSynchronized) {
			if(dbOpIdentifiers!=null){
				dbOpIdentifiers.close();
			}
			if(dbOpCapsulesTemporary!=null){
				dbOpCapsulesTemporary.close();
			}
		}
	}

}
